package com.party.admin.biz.copartner;

import com.party.common.utils.BigDecimalUtils;
import com.party.core.model.activity.Activity;
import com.party.core.model.copartner.TargetCopartner;
import com.party.core.model.member.Member;
import com.party.core.service.activity.IActivityService;
import com.party.core.service.copartner.ITargetCopartnerService;
import com.party.core.service.member.IMemberService;
import com.party.notify.notifyPush.officialPartner.ICopartnerNotifySendService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 目标与合伙人
 * Created by wei.li
 *
 * @date 2017/7/19 0019
 * @time 14:33
 */
@Service
public class TargetCopartnerBizService {

    @Autowired
    private ITargetCopartnerService targetCopartnerService;

    @Autowired
    private IMemberService memberService;

    @Autowired
    private IActivityService activityService;

    @Autowired
    private ICopartnerNotifySendService copartnerNotifySendService;

    Logger logger = LoggerFactory.getLogger(getClass());

    /**
     * 保存合伙人
     *
     * @param targetId    目标id
     * @param copartnerId 合伙人id
     * @param createdId   创建者id
     */
    public void saveCopartner(String targetId, String copartnerId, String createdId) {
        TargetCopartner copartner = targetCopartnerService.findByTargetId(targetId);
        if (copartner == null) {
            copartner = new TargetCopartner();
            copartner.setTargetId(targetId);
            copartner.setCreatedId(createdId);
            copartner.setPartnerId(copartnerId);
            targetCopartnerService.insert(copartner);
        } else {
            copartner.setCreatedId(createdId);
            copartner.setPartnerId(copartnerId);
            targetCopartnerService.update(copartner);
        }

        try {
            Member receiver = memberService.get(copartnerId);
            Activity activity = activityService.get(targetId);
            copartnerNotifySendService.sendAllotZcActivity(activity, receiver);
        } catch (Exception e) {
            logger.error("分配项目发送给合伙人推送异常", e);
        }
    }

    /**
     * 移除合伙人
     *
     * @param activityId 项目id
     */
    public void removeCopartner(String activityId) {
        TargetCopartner copartner = targetCopartnerService.findByTargetId(activityId);
        if (copartner != null) {
            targetCopartnerService.delete(copartner.getId());
        }
    }

    /**
     * 更新总佣金
     *
     * @param activityId 活动编号
     * @param brokerage 佣金
     */
    public void updateBrokerage(String activityId, double brokerage){
        TargetCopartner copartner = targetCopartnerService.findByTargetId(activityId);
        if (null != copartner ){
            if (null != copartner.getTotalBrokerage()){
                double add = BigDecimalUtils.add(copartner.getTotalBrokerage(), brokerage);
                double round = BigDecimalUtils.round(add, 2);
                copartner.setTotalBrokerage(round);
            }else {
                copartner.setTotalBrokerage(0d);
            }
            targetCopartnerService.update(copartner);
        }
    }
}
